<?php
namespace app\supplier\controller;
use app\supplier\controller\Base;
use app\supplier\model\SupplierModel;
use think\Session;

class Index extends Base
{
	
	/**
	 * 领域对象
	 * @return mixed
	 */
	public $supplier_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->supplier_obj = new SupplierModel(); 
	}
	
	
	/**
     * 后台首页
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
    public function index()
    {
		
        return view('index');
    }
	
	/**
     * 内容主体
     * @author 郑义生 <1052063941@qq.com>
     * @return mixed
     */
    public function home()
    {
		$input['id'] = Session::get('supplier_id');
		$view = SupplierModel::get($input['id'])->toArray();
		$city_list = Session::get('city_list');
		if(empty($city_list)){
			$api_url = config('api_url.url');
			$params = array();
			$params['PageSize'] = config('paginate.list_rows');
			$ret = FiltrsHttpJson($api_url.'/v1.0/city/lists','/v1.0/city/lists','1.0',$params);
			$city_list = $ret['ResponseData']['List'];
			Session::set('city_list',$city_list);
		}
		$pro_list = array();
		$cities_list = array();
		$distrinct = array();
		foreach($city_list as $_c){
			array_push($pro_list,array('id'=>$_c['id'],'name'=>$_c['name']));
			if($_c['id'] == $view['pro_id']){
				foreach($_c['cities'] as $_c_c){
					array_push($cities_list,array('id'=>$_c_c['id'],'name'=>$_c_c['name']));
					if($_c_c['id'] == $view['city_id']){
						foreach($_c_c['district'] as $_c_c_c){
							array_push($distrinct,array('id'=>$_c_c_c['id'],'name'=>$_c_c_c['name']));
						}
					}
				}
			}
		}
		$this->assign('pro_list', $pro_list);
		$this->assign('cities_list', $cities_list);
		$this->assign('distrinct', $distrinct);
		$this->assign('view', $view);
        return view('main');
    }
	
	/**
	 * 更新领域
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			if(isset($input['id']) && intval($input['id']) > 0){
				$input['update_time'] = time();
				$flag = $this->supplier_obj->save($input,['id'=>$input['id']]);
			}else{
				$input['create_time'] = time();
				$input['update_time'] = time();
				$this->supplier_obj->data($input);
				$flag = $this->supplier_obj->save();
			}
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新领域数据，请求不合法！');
		}
	}	
	
}
